﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PHRClient.DoctorForms.PatientRegister
{
    public partial class Settings : Form
    {
        private static Patient _patient;

        public Settings()
        {
            InitializeComponent();
            this.TopLevel = false;
            this.AutoScroll = true;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.Dock = DockStyle.Fill;
        }

        public Patient getPatientSecurity()
        {
            _patient = new Patient();

            bool isvalied = true;

            if (userNameTB.Text != "" && passwordTB.Text != "")
            {                
                _patient.UserName = userNameTB.Text;                

                if (passwordTB.Text.Equals(rePasswordTB.Text))
                    _patient.Password = passwordTB.Text;
                else
                {
                    isvalied = false;
                    MessageBox.Show("Password Information Incorrect, Check Again!!");
                }
            }
            else
            {
                isvalied = false;
                MessageBox.Show("Please Fill the Patient Security Information!!");
            }

            if (isvalied)
                return _patient;
            else return null;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //detination filename
            int newPatientId = RegisterPatient.newPatientId;
            string appPath = Path.GetDirectoryName(Application.ExecutablePath);

            string newFileName = @appPath + "\\Data\\patient" + newPatientId + ".jpg";

            //openFileDialog1.Filter = "JPEG Files (*.jpeg)|*.jpeg|PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif";
            openFileDialog1.Filter = "";

            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
            string sep = string.Empty;
            foreach (var c in codecs)
            {
               string codecName = c.CodecName.Substring(8).Replace("Codec", "Files").Trim();
               openFileDialog1.Filter = String.Format("{0}{1}{2} ({3})|{3}", openFileDialog1.Filter, sep, codecName, c.FilenameExtension);
               sep = "|";
            }
            openFileDialog1.Filter = String.Format("{0}{1}{2} ({3})|{3}", openFileDialog1.Filter, sep, "All Files", "*.*");
            openFileDialog1.DefaultExt = ".jpg"; 
            
            openFileDialog1.FileName = "";

            // if the user presses OK instead of Cancel
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                //get the selected filename
                string filename = openFileDialog1.FileName;

                //copy the file to the new filename location and overwrite if it already exists 
                //(set last parameter to false if you don't want to overwrite)
                System.IO.File.Copy(filename, newFileName, true);

                this.profPicBox.Image = Image.FromFile(newFileName);
            }
        }
    }
}
